<!-- Headers -->
<div class="container-fluid" style="padding:0;">
  <!-- Header -->
  <header class="header">
    <div class="page-header">
      <span class="logo glyphicon glyphicon-tasks"></span>
      <span class="title">项目列表</span>
    </div>
  </header>

  <!-- Sub Header -->
  <header class="header sub">
      <div class="row-fluid">
        <div class="toolbar">
          <button type="button" class="btn btn-success" id="btn-new" data-toggle="modal" data-target="#modal-project" data-backdrop="static" data-keyboard="false"><span class="glyphicon glyphicon-plus"></span> 新建项目</button>
          <button type="button" class="btn btn-default" id="btn-edit" disabled="disabled"><span class="glyphicon glyphicon-edit info"></span> 编辑</button>
          <button type="button" class="btn btn-default" id="btn-start" disabled="disabled"><span class="glyphicon glyphicon-play ok"></span> 开始</button>
          <button type="button" class="btn btn-default" id="btn-pause" disabled="disabled"><span class="glyphicon glyphicon-pause warning"></span> 暂停</button>
          <button type="button" class="btn btn-default" id="btn-restore" disabled="disabled"><span class="glyphicon glyphicon-repeat info"></span> 恢复</button>
          <button type="button" class="btn btn-default" id="btn-stop" disabled="disabled"><span class="glyphicon glyphicon-stop danger"></span> 结束</button>
          <button type="button" class="btn btn-default" id="btn-delete" disabled="disabled"><span class="glyphicon glyphicon-remove danger"></span> 删除</button>
        </div>
        <div class="toolbar pull-right">
          <button type="button" class="btn btn-default btn-xs" id="btn-refresh"><span class="glyphicon glyphicon-refresh"></span> 刷新</button>
          <button type="button" class="btn btn-default btn-xs"><span class="glyphicon glyphicon-cog"></span> 选项</button>
          <button type="button" class="btn btn-default btn-xs" data-toggle="modal" data-target="#help"><span class="glyphicon glyphicon-question-sign"></span> 帮助</button>
        </div>
      </div>
  </header>
</div>

<!-- Main component for a primary marketing message or call to action -->
<div class="container-fluid body">
  <table class="table table-striped table-bordered" id="projects">
    <thead>
      <tr>
        <th width="1%" class="nowrap center"><input type="checkbox" role="check-all"/></th>
        <th width="15%">项目名称</th>
        <th width="5%">状态</th>
        <th width="5%">进度</th>
        <th width="10%">创建时间</th>
        <th width="10%">更新时间</th>
        <th width="10%">开始时间</th>
        <th width="10%">结束时间</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td colspan="8" class="dataTables_empty">数据加载中...</td>
      </tr>
    </tbody>
  </table>
</div>

<!-- Modals -->
<!--- Project Add -->
<div class="modal fade" id="modal-project" role="dialog" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title">新建项目</h4>
      </div>
      <div class="modal-body">
        <div class="alert alert-success hide" for="alert"></div>
        <form method='post' action='#' class="form-horizontal" id="form-project">
          <input type="hidden" name="id">
          <div class="form-group">
            <label class="col-sm-2 control-label">项目名称</label>
            <div class="col-sm-10">
              <input type="text" class="form-control" name="name" placeholder="项目名称">
            </div>
          </div>
          <div class="form-group">
            <label class="col-sm-2 control-label">项目描述</label>
            <div class="col-sm-10">
              <textarea class="form-control" rows="3" name="desc" placeholder="填写项目描述信息"></textarea>
            </div>
          </div>
        </form>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-primary" role="submit">&nbsp;确&nbsp;&nbsp;定&nbsp;</button>
        <button type="button" class="btn btn-danger" data-dismiss="modal">&nbsp;取&nbsp;&nbsp;消&nbsp;</button>
      </div>
    </div>
  </div>
</div>
<!-- Help -->
<div class="modal fade" id="help" role="dialog" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title">帮助</h4>
      </div>
      <div class="modal-body">
        <h4>项目状态</h4><hr>
        <p><span class="label label-success">正常</span>  项目正常进行中！</p>
        <p><span class="label label-warning">暂停</span>  暂停项目将不可计划相关操作且员工不可登陆。</p><br>
        <h4>项目进度</h4><hr>
        <p><span class="label label-info">筹备中</span>  项目筹备中，需要计划员完善材料计划！</p>
        <p><span class="label label-success">进行中</span>  项目进行中，不可修改，计划可提交。</p>
        <p><span class="label label-default">已结束</span>  项目已结束不可对项目进行任何操作。</p><br >

        <h4>其他说明</h4><hr>
        <p>若项目状态暂停，项目将不能做任何操作。</p>
      </div>
    </div>
  </div>
</div>
<link rel="stylesheet" type="text/css" href="css/jquery.table.min.css">
<script type="text/javascript" language="javascript" src="js/jquery.cookie.min.js"></script>
<script type="text/javascript" language="javascript" src="js/jquery.table.min.js"></script>
<script type="text/javascript" charset="utf-8">
  
  function project_detail(id) {
    $.cookie('project', id);
    load_content('project/detail.html');
    return false;
  }

  function change_selection() {
    var total = $("#projects").find('td').find(':input[name=id]').length;
    var selected = $("#projects").find('td').find(':checked').length;
    if (total == selected) $("#projects").find('input[role="check-all"]').prop('checked', true);
    else $("#projects").find('input[role="check-all"]').prop('checked', false);
    if (selected == 1) {
      var project = $("#projects").find('td').find(':checked');
      var status = project.attr('data-status');
      var process = project.attr('data-process');
      $("#btn-edit").attr('disabled', process == 0 ? false : true);
      $("#btn-start").attr('disabled', process == 0 ? (status == 1 ? false : true) : true); 
      $("#btn-pause").attr('disabled', status == 1 ? (process == -1 ? true : false) : true ); 
      $("#btn-restore").attr('disabled', status == 0 ? false : true); 
      $("#btn-stop").attr('disabled', process == 1 ? (status == 1 ? false : true) : true); 
      $("#btn-delete").attr('disabled', process == 1 ? true : false);
    } else if (selected > 1) {
      $("#btn-edit").attr('disabled',true); 
      $("#btn-start").attr('disabled',true); 
      $("#btn-pause").attr('disabled',true); 
      $("#btn-restore").attr('disabled',true); 
      $("#btn-stop").attr('disabled',true); 
      $("#btn-delete").attr('disabled',false); 
    } else {
      $("#btn-edit").attr('disabled',true); 
      $("#btn-start").attr('disabled',true); 
      $("#btn-pause").attr('disabled',true); 
      $("#btn-restore").attr('disabled',true); 
      $("#btn-stop").attr('disabled',true); 
      $("#btn-delete").attr('disabled',true); 
    }
    return false;
  }

  $( document ).ready(function() {
    var table = $('#projects').dataTable( {
      "bProcessing": false,
      "bServerSide": true,
      "bFilter": false,
      "nPaging": true,
      "bSort": false,
      "bLengthChange": false,
      "fnFailed": function (error, thrown) {
        $.global.error = true;
        $("#modal-load").find("img").attr('src', 'images/warn.png');
        $("#modal-load").find("span").attr('class', 'label label-warning').html('内容加载失败，请稍后再试。');
        setTimeout(function(){$('#modal-load').modal('hide')} , $.global.timeout);
      },
      "fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
        $('td:eq(0)', nRow).html( '<input type="checkbox" name="id" value="' + aData[0] + '" onclick="change_selection();" data-status="' + aData[2] + '" data-process="' + aData[3] + '">' );
        $('td:eq(1)', nRow).html('<a href="#" onclick="project_detail(' + aData[0] + ');">' + aData[1] + '</a>');
        
        // Status
        if (aData[2] == 1) {
          $('td:eq(2)', nRow).html( '<span class="label label-success">正常</span>' );
        } else if (aData[2] == 0) {
          $('td:eq(2)', nRow).html( '<span class="label label-warning">暂停</span>' );
        } else {
          $('td:eq(2)', nRow).html( '<span class="label label-danger">异常</span>' );
        }
        // Process
        if (aData[3] == 0) {
          $('td:eq(3)', nRow).html( '<span class="label label-info">筹备中</span>' );
        } else if (aData[3] == 1) {
          $('td:eq(3)', nRow).html( '<span class="label label-success">进行中</span>' );
        } else if (aData[3] == -1) {
          $('td:eq(3)', nRow).html( '<span class="label label-default">已结束</span>' );
        } else {
          $('td:eq(3)', nRow).html( '<span class="label label-danger">进度异常</span>' );
        }
        // Fix Info
        if (aData[4] == '1000-01-01 00:00:00') $('td:eq(4)', nRow).html( '<span class="label label-default">数据异常</span>' );
        if (aData[5] == '1000-01-01 00:00:00') $('td:eq(5)', nRow).html( '<span class="label label-default">从未更新</span>' );
        if (aData[6] == '1000-01-01 00:00:00') $('td:eq(6)', nRow).html( '<span class="label label-default">尚未开始</span>' );
        if (aData[7] == '1000-01-01 00:00:00') $('td:eq(7)', nRow).html( '<span class="label label-default">尚未结束</span>' );
        
      },
      "oLanguage": {
        "sProcessing":   "处理中...",
        "sLengthMenu":   "显示 _MENU_ 项结果",
        "sZeroRecords":  "没有匹配结果",
        "sInfo":         "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
        "sInfoEmpty":    "显示第 0 至 0 项结果，共 0 项",
        "sInfoFiltered": "(由 _MAX_ 项结果过滤)",
        "sInfoPostFix":  "",
        "sSearch":       "搜索:",
        "sUrl":          "",
        "oPaginate": {
          "sFirst":    "<span class='glyphicon glyphicon-backward'></span>",
          "sPrevious": "<span class='glyphicon glyphicon-chevron-left'></span>",
          "sNext":     "<span class='glyphicon glyphicon-chevron-right'></span>",
          "sLast":     "<span class='glyphicon glyphicon-forward'></span>"
        }
      },
      "sAjaxSource": "project/find"
    } );

    var fn_selected_projects = function () {
      var ids = [];
      $("#projects").find('td').find(':checked').each(function() {
        ids.push($(this).attr('value'));
      });
      return ids;
    }

    var fn_refresh_table = function() {
      $("#projects").find('input[role="check-all"]').prop('checked', false);
      $("#btn-edit").attr('disabled',true); 
      $("#btn-start").attr('disabled',true); 
      $("#btn-pause").attr('disabled',true); 
      $("#btn-restore").attr('disabled',true); 
      $("#btn-stop").attr('disabled',true); 
      $("#btn-delete").attr('disabled',true); 
      $.global.error = false;
      $("#modal-load").find("img").attr('src', 'images/loader.gif');
      $("#modal-load").find("span").attr('class', 'label label-info').html('内容加载中，请稍后...'); 
      table.fnReloadAjax();
    }

    var fn_start_project = function(id) {
      $.ajax({
        type: 'post',
        url: 'project/start',
        data: 'id=' + id,
        success: function(data, status) {
          if (data == undefined || data.status == undefined || data.status == 4) {
            $.global.error = true;
            $("#modal-load").find("img").attr('src', 'images/warn.png');
            $("#modal-load").find("span").attr('class', 'label label-warning').html('服务端异常，请联系管理员！');
            setTimeout(function(){fn_refresh_table()} , $.global.timeout);
          } else {
            if (data.status == -1) {
              $.global.error = true;
              $("#modal-load").find("img").attr('src', 'images/cross.png');
              $("#modal-load").find("span").attr('class', 'label label-danger').html('项目已结束！');
              setTimeout(function(){fn_refresh_table()} , $.global.timeout);
            } else if (data.status == 1) {
              $.global.error = true;
              $("#modal-load").find("img").attr('src', 'images/warn.png');
              $("#modal-load").find("span").attr('class', 'label label-warning').html('项目进行中。。。');
              setTimeout(function(){fn_refresh_table()} , $.global.timeout);
            } else if (data.status == 2) {
              $.global.error = true;
              $("#modal-load").find("img").attr('src', 'images/warn.png');
              $("#modal-load").find("span").attr('class', 'label label-warning').html('项目状态错误，不可用！');
              setTimeout(function(){fn_refresh_table()} , $.global.timeout);
            } else if (data.status == 3) {
              $.global.error = true;
              $("#modal-load").find("img").attr('src', 'images/warn.png');
              $("#modal-load").find("span").attr('class', 'label label-warning').html('项目暂停，请先恢复项目状态。');
              setTimeout(function(){fn_refresh_table()} , $.global.timeout);
            } else if (data.status == 0) {
              $.global.error = true;
              $("#modal-load").find("img").attr('src', 'images/accept.png');
              $("#modal-load").find("span").attr('class', 'label label-success').html('操作成功，刷新数据。');
              setTimeout(function(){fn_refresh_table()} , $.global.timeout);
            } else {
              $.global.error = true;
              $("#modal-load").find("img").attr('src', 'images/warn.png');
              $("#modal-load").find("span").attr('class', 'label label-warning').html('服务端异常，操作失败！');
              setTimeout(function(){fn_refresh_table()} , $.global.timeout);
            }
          }
          //fn_refresh_table();
        },
        error: function(hxr, status, error) {
          $.global.error = true;
          $("#modal-load").find("img").attr('src', 'images/warn.png');
          $("#modal-load").find("span").attr('class', 'label label-warning').html('服务端异常，操作失败！');
          setTimeout(function(){fn_refresh_table()} , $.global.timeout);
        }
      });
    }

    var fn_pause_project = function(id) {
      $.ajax({
        type: 'post',
        url: 'project/pause',
        data: 'id=' + id,
        success: function(data, status) {
          if (data == undefined || data.status == undefined || data.status == 4) {
            $.global.error = true;
            $("#modal-load").find("img").attr('src', 'images/warn.png');
            $("#modal-load").find("span").attr('class', 'label label-warning').html('服务端异常，请联系管理员！');
            setTimeout(function(){fn_refresh_table()} , $.global.timeout);
          } else {
            if (data.status == -1) {
              $.global.error = true;
              $("#modal-load").find("img").attr('src', 'images/cross.png');
              $("#modal-load").find("span").attr('class', 'label label-danger').html('项目已结束！');
              setTimeout(function(){fn_refresh_table()} , $.global.timeout);
            } else if (data.status == 1) {
              $.global.error = true;
              $("#modal-load").find("img").attr('src', 'images/warn.png');
              $("#modal-load").find("span").attr('class', 'label label-warning').html('项目尚未开始。。。');
              setTimeout(function(){fn_refresh_table()} , $.global.timeout);
            } else if (data.status == 2) {
              $.global.error = true;
              $("#modal-load").find("img").attr('src', 'images/warn.png');
              $("#modal-load").find("span").attr('class', 'label label-warning').html('项目状态错误，不可用！');
              setTimeout(function(){fn_refresh_table()} , $.global.timeout);
            } else if (data.status == 3) {
              $.global.error = true;
              $("#modal-load").find("img").attr('src', 'images/warn.png');
              $("#modal-load").find("span").attr('class', 'label label-warning').html('项目暂停，请先恢复项目状态。');
              setTimeout(function(){fn_refresh_table()} , $.global.timeout);
            } else if (data.status == 0) {
              $.global.error = true;
              $("#modal-load").find("img").attr('src', 'images/accept.png');
              $("#modal-load").find("span").attr('class', 'label label-success').html('操作成功，刷新数据。');
              setTimeout(function(){fn_refresh_table()} , $.global.timeout);
            } else {
              $.global.error = true;
              $("#modal-load").find("img").attr('src', 'images/warn.png');
              $("#modal-load").find("span").attr('class', 'label label-warning').html('服务端异常，操作失败！');
              setTimeout(function(){fn_refresh_table()} , $.global.timeout);
            }
          }
          //fn_refresh_table();
        },
        error: function(hxr, status, error) {
          $.global.error = true;
          $("#modal-load").find("img").attr('src', 'images/warn.png');
          $("#modal-load").find("span").attr('class', 'label label-warning').html('服务端异常，操作失败！');
          setTimeout(function(){fn_refresh_table()} , $.global.timeout);
        }
      });
    }

    var fn_restore_project = function(id) {
      $.ajax({
        type: 'post',
        url: 'project/restore',
        data: 'id=' + id,
        success: function(data, status) {
          if (data == undefined || data.status == undefined || data.status == 4) {
            $.global.error = true;
            $("#modal-load").find("img").attr('src', 'images/warn.png');
            $("#modal-load").find("span").attr('class', 'label label-warning').html('服务端异常，请联系管理员！');
            setTimeout(function(){fn_refresh_table()} , $.global.timeout);
          } else {
            if (data.status == -1) {
              $.global.error = true;
              $("#modal-load").find("img").attr('src', 'images/cross.png');
              $("#modal-load").find("span").attr('class', 'label label-danger').html('项目已结束！');
              setTimeout(function(){fn_refresh_table()} , $.global.timeout);
            } else if (data.status == 1) {
              $.global.error = true;
              $("#modal-load").find("img").attr('src', 'images/warn.png');
              $("#modal-load").find("span").attr('class', 'label label-warning').html('项目尚未开始。。。');
              setTimeout(function(){fn_refresh_table()} , $.global.timeout);
            } else if (data.status == 2) {
              $.global.error = true;
              $("#modal-load").find("img").attr('src', 'images/warn.png');
              $("#modal-load").find("span").attr('class', 'label label-warning').html('项目状态错误，不可用！');
              setTimeout(function(){fn_refresh_table()} , $.global.timeout);
            } else if (data.status == 3) {
              $.global.error = true;
              $("#modal-load").find("img").attr('src', 'images/warn.png');
              $("#modal-load").find("span").attr('class', 'label label-warning').html('项目暂停，请先恢复项目状态。');
              setTimeout(function(){fn_refresh_table()} , $.global.timeout);
            } else if (data.status == 0) {
              $.global.error = true;
              $("#modal-load").find("img").attr('src', 'images/accept.png');
              $("#modal-load").find("span").attr('class', 'label label-success').html('操作成功，刷新数据。');
              setTimeout(function(){fn_refresh_table()} , $.global.timeout);
            } else {
              $.global.error = true;
              $("#modal-load").find("img").attr('src', 'images/warn.png');
              $("#modal-load").find("span").attr('class', 'label label-warning').html('服务端异常，操作失败！');
              setTimeout(function(){fn_refresh_table()} , $.global.timeout);
            }
          }
          //fn_refresh_table();
        },
        error: function(hxr, status, error) {
          $.global.error = true;
          $("#modal-load").find("img").attr('src', 'images/warn.png');
          $("#modal-load").find("span").attr('class', 'label label-warning').html('服务端异常，操作失败！');
          setTimeout(function(){fn_refresh_table()} , $.global.timeout);
        }
      });
    }

    var fn_stop_project = function(id) {
      $.ajax({
        type: 'post',
        url: 'project/stop',
        data: 'id=' + id,
        success: function(data, status) {
          if (data == undefined || data.status == undefined || data.status == 4) {
            $.global.error = true;
            $("#modal-load").find("img").attr('src', 'images/warn.png');
            $("#modal-load").find("span").attr('class', 'label label-warning').html('服务端异常，请联系管理员！');
            setTimeout(function(){fn_refresh_table()} , $.global.timeout);
          } else {
            if (data.status == -1) {
              $.global.error = true;
              $("#modal-load").find("img").attr('src', 'images/cross.png');
              $("#modal-load").find("span").attr('class', 'label label-danger').html('项目已结束！');
              setTimeout(function(){fn_refresh_table()} , $.global.timeout);
            } else if (data.status == 1) {
              $.global.error = true;
              $("#modal-load").find("img").attr('src', 'images/warn.png');
              $("#modal-load").find("span").attr('class', 'label label-warning').html('项目尚未开始。。。');
              setTimeout(function(){fn_refresh_table()} , $.global.timeout);
            } else if (data.status == 2) {
              $.global.error = true;
              $("#modal-load").find("img").attr('src', 'images/warn.png');
              $("#modal-load").find("span").attr('class', 'label label-warning').html('项目状态错误，不可用！');
              setTimeout(function(){fn_refresh_table()} , $.global.timeout);
            } else if (data.status == 3) {
              $.global.error = true;
              $("#modal-load").find("img").attr('src', 'images/warn.png');
              $("#modal-load").find("span").attr('class', 'label label-warning').html('项目暂停，请先恢复项目状态。');
              setTimeout(function(){fn_refresh_table()} , $.global.timeout);
            } else if (data.status == 0) {
              $.global.error = true;
              $("#modal-load").find("img").attr('src', 'images/accept.png');
              $("#modal-load").find("span").attr('class', 'label label-success').html('操作成功，刷新数据。');
              setTimeout(function(){fn_refresh_table()} , $.global.timeout);
            } else {
              $.global.error = true;
              $("#modal-load").find("img").attr('src', 'images/warn.png');
              $("#modal-load").find("span").attr('class', 'label label-warning').html('服务端异常，操作失败！');
              setTimeout(function(){fn_refresh_table()} , $.global.timeout);
            }
          }
          //fn_refresh_table();
        },
        error: function(hxr, status, error) {
          $.global.error = true;
          $("#modal-load").find("img").attr('src', 'images/warn.png');
          $("#modal-load").find("span").attr('class', 'label label-warning').html('服务端异常，操作失败！');
          setTimeout(function(){fn_refresh_table()} , $.global.timeout);
        }
      });
    }

    var fn_delete_projects = function(ids) {
      $.ajax({
        type: 'post',
        url: 'project/delete',
        data: 'ids=' + ids,
        success: function(data, status) {
          if (data == undefined || data.status == undefined || data.status == 4) {
            $.global.error = true;
            $("#modal-load").find("img").attr('src', 'images/warn.png');
            $("#modal-load").find("span").attr('class', 'label label-warning').html('服务端异常，请联系管理员！');
            setTimeout(function(){fn_refresh_table()} , $.global.timeout);
          } else {
            if (data.status == -1) {
              $.global.error = true;
              $("#modal-load").find("img").attr('src', 'images/cross.png');
              $("#modal-load").find("span").attr('class', 'label label-danger').html('操作失败，非法操作！');
              setTimeout(function(){fn_refresh_table()} , $.global.timeout);
            } else if (data.status == 0) {
              $.global.error = true;
              $("#modal-load").find("img").attr('src', 'images/accept.png');
              $("#modal-load").find("span").attr('class', 'label label-success').html('操作成功，刷新数据。');
              setTimeout(function(){fn_refresh_table()} , $.global.timeout);
            } else {
              $.global.error = true;
              $("#modal-load").find("img").attr('src', 'images/warn.png');
              $("#modal-load").find("span").attr('class', 'label label-warning').html('服务端异常，操作失败！');
              setTimeout(function(){fn_refresh_table()} , $.global.timeout);
            }
          }
          //fn_refresh_table();
        },
        error: function(hxr, status, error) {
          $.global.error = true;
          $("#modal-load").find("img").attr('src', 'images/warn.png');
          $("#modal-load").find("span").attr('class', 'label label-warning').html('服务端异常，操作失败！');
          setTimeout(function(){fn_refresh_table()} , $.global.timeout);
        }
      });
    }
    $('#btn-new').click(function() {
      $("#modal-project").find('div[for=alert]').attr('class', 'alert alert-success').html('').hide();
      $("#modal-project").find(".modal-title").html("新建项目");
      $("#form-project").attr('action', 'project/add');
      $("#form-project").find(':input[name=id]').val('');
      $("#form-project").find(':input[name=name]').val('');
      $("#form-project").find(':input[name=desc]').val('');       
    });
    $('#btn-edit').click(function() {
      $("#modal-project").find('div[for=alert]').attr('class', 'alert alert-success').html('').hide();
      $("#form-project").attr('action', 'project/edit');
      $("#form-project").find(':input[name=id]').val('');
      $("#form-project").find(':input[name=name]').val('');
      $("#form-project").find(':input[name=desc]').val('');  
      // load project
      $.ajax({
        url: "project/detail",
        data: 'id=' + fn_selected_projects()[0],
        success: function(data, status) {
          if (data == undefined || data.project == undefined) {
            $.global.error = true;
            $("#modal-load").find("img").attr('src', 'images/warn.png');
            $("#modal-load").find("span").attr('class', 'label label-warning').html('项目详情加载失败，请稍后再试。');
            setTimeout(function(){$('#modal-load').modal('hide')} , $.global.timeout);
          } else {
            $.global.error = false;
            $("#form-project").find(':input[name=id]').val(data.project.id);
            $("#form-project").find(':input[name=name]').val(data.project.name);
            $("#form-project").find(':input[name=desc]').val(data.project.desc);
            $("#modal-project").find(".modal-title").html("修改项目信息");
            $('#modal-project').modal({backdrop: 'static',show:true, keyboard: false});
          }
        },
        error: function(hxr, status, error) {
          $("#modal-load").find("img").attr('src', 'images/warn.png');
          $("#modal-load").find("span").attr('class', 'label label-warning').html('项目详情加载失败，请稍后再试。');
          setTimeout(function(){$('#modal-load').modal('hide')} , $.global.timeout);
        }
      });     
    });
    $('#btn-start').click(function() {
      fn_start_project(fn_selected_projects()[0]);
    });
    $('#btn-pause').click(function() {
      fn_pause_project(fn_selected_projects()[0]);
    });
    $('#btn-restore').click(function() {
      fn_restore_project(fn_selected_projects()[0]);
    });
    $('#btn-stop').click(function() {
      fn_stop_project(fn_selected_projects()[0]);
    });
    // deleted projects
    $('#btn-delete').click(function() {
      fn_delete_projects(fn_selected_projects());
    });
    // refresh
    $('#btn-refresh').click(fn_refresh_table);
    $("#form-project").submit(function() {
      return false;
    });
    $("#modal-project").find(':input[role=submit]').click(function() {
      var form = $("#form-project");
      $.ajax({
        type: form.attr('method'),
        url: form.attr('action'),
        data: form.serialize(),
        success: function(data, status) {
          if (data == undefined || data.status == undefined || data.status != 0) {
            if (data == undefined || data.status == undefined) {
                $("#modal-project").find('div[for=alert]').attr('class', 'alert alert-danger').html('服务端错误，请联系管理员！').show();
            } else {
              if (data.status == 1) {
                $("#modal-project").find('div[for=alert]').attr('class', 'alert alert-warning').html('输入信息有误！').show();
              } else {
                $("#modal-project").find('div[for=alert]').attr('class', 'alert alert-danger').html(data.msg == undefined ? '服务端异常，项目添加失败！' : data.msg).show();
              }
            }
          } else {
            if ($("#form-project").attr('action') == 'project/edit') {
              fn_refresh_table();
              $("#modal-project").find('div[for=alert]').attr('class', 'alert alert-success').html('项目信息修改成功！').show();
            } else {
              $("#modal-project").find('div[for=alert]').attr('class', 'alert alert-success').html('项目新建成功！').show();
              fn_refresh_table();
              form.find(':input[name=name]').val('');
              form.find(':input[name=desc]').val('');           
            }
          }
        },
        error: function(hxr, status, error) {
          $("#modal-project").find('div[for=alert]').attr('class', 'alert alert-danger').html('服务端异常，项目创建失败！').show();
        }
      });
      return false;
    });


    $("#projects").find('input[role="check-all"]').change(function() {
      if ($(this).prop('checked')) {
        $("#projects").find('tbody').find('input[name="id"]').prop('checked', true);
        $("#btn-edit").attr('disabled',true); 
        $("#btn-start").attr('disabled',true); 
        $("#btn-pause").attr('disabled',true); 
        $("#btn-restore").attr('disabled',true); 
        $("#btn-stop").attr('disabled',true); 
        $("#btn-delete").attr('disabled',fn_selected_projects().length > 0 ? false : true); 
      } else {
        $("#projects").find('tbody').find('input[name="id"]').prop('checked', false);
        $("#btn-edit").attr('disabled',true); 
        $("#btn-start").attr('disabled',true); 
        $("#btn-pause").attr('disabled',true); 
        $("#btn-restore").attr('disabled',true); 
        $("#btn-stop").attr('disabled',true); 
        $("#btn-delete").attr('disabled',true); 
      }
    });
  });
</script>